package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.domain.SaleDispatchDetails;
import com.mjf.cjt.domain.SaleDispatch;
import com.mjf.cjt.mapper.SaleDispatchMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.SaleDispatchDetailsService;
import com.mjf.cjt.service.SaleDispatchService;
import com.mjf.cjt.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售出库单列表查询
 * </p>
 *
 * @author
 * @since 2025-04-05
 */
@Service
public class SaleDispatchServiceImpl extends ServiceImpl<SaleDispatchMapper, SaleDispatch> implements SaleDispatchService {

    @Autowired
    private AsyncTMService asyncTMService;

    @Autowired
    private SaleDispatchDetailsService saleDispatchDetailsService;
    @Override
    public Page<SaleDispatchListVo> querySaleDispatch(SaleDispatchQueryParam param) {
        Page<SaleDispatchListVo> page = new Page<>(param.getPage(), param.getSize());
        page.setTotal(0);
        page.setRecords(new ArrayList<>());
        LambdaQueryWrapper<SaleDispatch> qw = Wrappers.<SaleDispatch>lambdaQuery()
                .ge(StrUtil.isNotBlank(param.getStart()), SaleDispatch::getCreatedTime, param.getStart())
                .le(StrUtil.isNotBlank(param.getEnd()), SaleDispatch::getCreatedTime, param.getEnd())
                ;
        long count = count(qw);
        page.setTotal(count);
        qw.orderByDesc(SaleDispatch::getCreatedTime);
        qw.last("limit " + (param.getPage() - 1) * param.getSize() + "," + param.getSize());

        List<SaleDispatch> list = list(qw);

        page.setRecords(list.stream().map(item -> SaleDispatchListVo.builder()
                        .businessTypeCode(item.getBusinessTypeCode())
                        .businessTypeName(item.getBusinessTypeName())
                        .code(item.getCode())
                        .voucherdate(item.getVoucherdate())
                        .createdTime(item.getCreatedTime())
                        .socialCollaborateFlag(item.getSocialCollaborateFlag())
                        .warehouseName(item.getWarehouseName())
                        .receiveWarehouseName(item.getReceiveWarehouseName())
                        .currencyName(item.getCurrencyName())
                        .exchangeRate(item.getExchangeRate())
                        .rdstyleName(item.getRdstyleName())
                        .partnerCode(item.getPartnerCode())
                        .partnerName(item.getPartnerName())
                        .partnerAbbName(item.getPartnerAbbName())
                        .partnerPriuserdefnvc1(item.getPartnerPriuserdefnvc1())
                        .partnerPriuserdefnvc2(item.getPartnerPriuserdefnvc2())
                        .partnerPriuserdefnvc3(item.getPartnerPriuserdefnvc3())
                        .partnerPriuserdefnvc4(item.getPartnerPriuserdefnvc4())
                        .partnerPriuserdefnvc5(item.getPartnerPriuserdefnvc5())
                        .partnerPriuserdefdecm1(item.getPartnerPriuserdefdecm1())
                        .partnerPriuserdefdecm2(item.getPartnerPriuserdefdecm2())
                        .partnerPriuserdefdecm3(item.getPartnerPriuserdefdecm3())
                        .partnerPriuserdefdecm4(item.getPartnerPriuserdefdecm4())
                        .partnerPriuserdefdecm5(item.getPartnerPriuserdefdecm5())
                        .settleCustomerBame(item.getSettleCustomerBame())
                        .memberCode(item.getMemberCode())
                        .memberCardcode(item.getMemberCardcode())
                        .memberName(item.getMemberName())
                        .mobilephone(item.getMobilephone())
                        .address(item.getAddress())
                        .mmberBalanceIntegral(item.getMmberBalanceIntegral())
                        .deptName(item.getDeptName())
                        .storeName(item.getStoreName())
                        .clerkName(item.getClerkName())
                        .projectName(item.getProjectName())
                        .deliverymodeName(item.getDeliverymodeName())
                        .dispatchAddress(item.getDispatchAddress())
                        .provinceName(item.getProvinceName())
                        .cityName(item.getCityName())
                        .districtName(item.getDistrictName())
                        .logisticsName(item.getLogisticsName())
                        .transdocno(item.getTransdocno())
                        .deliverycode(item.getDeliverycode())
                        .deliveryVoucherDate(item.getDeliveryVoucherDate())
                        .transDriverName(item.getTransDriverName())
                        .transDriverPhone(item.getTransDriverPhone())
                        .transvehicleInfo(item.getTransvehicleInfo())
                        .logisticsFee(item.getLogisticsFee())
                        .logisticsSettName(item.getLogisticsSettName())
                        .logisticsStatusName(item.getLogisticsStatusName())
                        .linkman(item.getLinkman())
                        .contactPhone(item.getContactPhone())
                        .customerPhone(item.getCustomerPhone())
                        .examinerName(item.getExaminerName())
                        .examinerDate(item.getExaminerDate())
                        .consignorName(item.getConsignorName())
                        .consignorDate(item.getConsignorDate())
                        .sourceVoucherTypeName(item.getSourceVoucherTypeName())
                        .sourceVoucherTypeCode(item.getSourceVoucherTypeCode())
                        .datasourceName(item.getDatasourceName())
                        .saleorderCode(item.getSaleorderCode())
                        .collaborateVoucherCode(item.getCollaborateVoucherCode())
                        .isstockRequestautoTransfe(item.getIsstockRequestautoTransfe())
                        .memo(item.getMemo())
                        .maker(item.getMaker())
                        .agency(item.getAgency())
                        .auditor(item.getAuditor())
                        .auditedDate(item.getAuditedDate())
                        .auditedTime(item.getAuditedTime())
                        .printCount(item.getPrintCount())
                        .reviser(item.getReviser())
                        .amount(item.getAmount())
                        .totalTaxAmount(item.getTotalTaxAmount())
                        .voucherstateName(item.getVoucherstateName())
                        .docId(item.getDocId())
                        .externalCode(item.getExternalCode())
                        .deliverystateName(item.getDeliverystateName())
                        .priuserdefnvc1(item.getPriuserdefnvc1())
                        .priuserdefnvc2(item.getPriuserdefnvc2())
                        .priuserdefnvc3(item.getPriuserdefnvc3())
                        .priuserdefnvc4(item.getPriuserdefnvc4())
                        .priuserdefnvc5(item.getPriuserdefnvc5())
                        .priuserdefnvc6(item.getPriuserdefnvc6())
                        .priuserdefdecm1(item.getPriuserdefdecm1())
                        .priuserdefdecm2(item.getPriuserdefdecm2())
                        .priuserdefdecm3(item.getPriuserdefdecm3())
                        .priuserdefdecm4(item.getPriuserdefdecm4())
                        .priuserdefdecm5(item.getPriuserdefdecm5())
                        .priuserdefdecm6(item.getPriuserdefdecm6())
                        .ts(item.getTs())
                        .build())
                .collect(Collectors.toList()));
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getSaleDispatchOrderFromRemote(String start, String end) {

        try {
            List<SaleDispatchVo> saleDispatchRemote = asyncTMService.getSaleDispatchOrderFromRemote(start, end);
            if (CollectionUtil.isNotEmpty(saleDispatchRemote)) {
                // 按明细行ID分组
                Map<String, List<SaleDispatchVo>> voMap =
                        saleDispatchRemote.stream().collect(Collectors.groupingBy(SaleDispatchVo::getId));

                List<SaleDispatch> saleDispatchPageList = new ArrayList<>();

                List<SaleDispatchDetails> saleDispatchDetails = new ArrayList<>();

                for (Map.Entry<String, List<SaleDispatchVo>> entry : voMap.entrySet()) {

                    // 主单
                    SaleDispatch order = new SaleDispatch();
                    order.setBusinessTypeCode(entry.getValue().get(0).getBusitype_code());
                    order.setBusinessTypeName(entry.getValue().get(0).getBusitype_name());
                    order.setCode(entry.getValue().get(0).getCode());
                    order.setRdrecordId(entry.getValue().get(0).getId());
                    order.setVoucherdate(entry.getValue().get(0).getVoucherdate());
                    order.setCreatedTime(entry.getValue().get(0).getCreatedtime());
                    order.setSocialCollaborateFlag(entry.getValue().get(0).getSocialcollaborateflag());
                    order.setSocialCollaborateStatusName(entry.getValue().get(0).getSocialcollaboratestatus_name());
                    order.setWarehouseName(entry.getValue().get(0).getWarehouse_name());
                    order.setReceiveWarehouseName(entry.getValue().get(0).getReceivewarehouse_name());
                    order.setCurrencyName(entry.getValue().get(0).getCurrency_name());
                    order.setExchangeRate(entry.getValue().get(0).getExchangerate());
                    order.setRdstyleName(entry.getValue().get(0).getRdstyle_name());
                    order.setPartnerCode(entry.getValue().get(0).getPartner_code());
                    order.setPartnerName(entry.getValue().get(0).getPartner_name());
                    order.setPartnerAbbName(entry.getValue().get(0).getPartner_partnerabbname());
                    order.setPartnerPriuserdefnvc1(entry.getValue().get(0).getPartner_priuserdefnvc1());
                    order.setPartnerPriuserdefnvc2(entry.getValue().get(0).getPartner_priuserdefnvc2());
                    order.setPartnerPriuserdefnvc3(entry.getValue().get(0).getPartner_priuserdefnvc3());
                    order.setPartnerPriuserdefnvc4(entry.getValue().get(0).getPartner_priuserdefnvc4());
                    order.setPartnerPriuserdefnvc5(entry.getValue().get(0).getPartner_priuserdefnvc5());
                    order.setPartnerPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPartner_priuserdefdecm1())? null : new BigDecimal(entry.getValue().get(0).getPartner_priuserdefdecm1()));
                    order.setPartnerPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPartner_priuserdefdecm2())? null : new BigDecimal(entry.getValue().get(0).getPartner_priuserdefdecm2()));
                    order.setPartnerPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPartner_priuserdefdecm3())? null : new BigDecimal(entry.getValue().get(0).getPartner_priuserdefdecm3()));
                    order.setPartnerPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPartner_priuserdefdecm4())? null : new BigDecimal(entry.getValue().get(0).getPartner_priuserdefdecm4()));
                    order.setPartnerPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPartner_priuserdefdecm5())? null : new BigDecimal(entry.getValue().get(0).getPartner_priuserdefdecm5()));
                    order.setSettleCustomerBame(entry.getValue().get(0).getSettlecustomer_name());
                    order.setMemberCode(entry.getValue().get(0).getMember_code());
                    order.setMemberCardcode(entry.getValue().get(0).getMember_cardcode());
                    order.setMemberName(entry.getValue().get(0).getMember_name());
                    order.setMobilephone(entry.getValue().get(0).getMobilephone());
                    order.setAddress(entry.getValue().get(0).getAddress());
                    order.setMmberBalanceIntegral(entry.getValue().get(0).getMember_balanceintegral());
                    order.setDeptName(entry.getValue().get(0).getDepartment_name());
                    order.setStoreName(entry.getValue().get(0).getStore_name());
                    order.setClerkName(entry.getValue().get(0).getClerk_name());
                    order.setProjectName(entry.getValue().get(0).getProject_name());
                    order.setDeliverymodeName(entry.getValue().get(0).getTransporttype_name());
                    order.setDispatchAddress(entry.getValue().get(0).getDispatchaddress());
                    order.setProvinceName(entry.getValue().get(0).getProvince_name());
                    order.setCityName(entry.getValue().get(0).getCity_name());
                    order.setDistrictName(entry.getValue().get(0).getDistrictaddress_name());
                    order.setLogisticsName(entry.getValue().get(0).getLogisticsnetwork_name());
                    order.setTransdocno(entry.getValue().get(0).getTransdocno());
                    order.setDeliverycode(entry.getValue().get(0).getDeliverycode());
                    order.setDeliveryVoucherDate(entry.getValue().get(0).getDeliveryvoucherdate());
                    order.setTransDriverName(entry.getValue().get(0).getTransdrivername());
                    order.setTransDriverPhone(entry.getValue().get(0).getTransdriverphone());
                    order.setTransvehicleInfo(entry.getValue().get(0).getTransvehicleinfo());
                    order.setLogisticsFee(entry.getValue().get(0).getLogisticsfee());
                    order.setLogisticsSettName(entry.getValue().get(0).getLogisticssettlementmodes_name());
                    order.setLogisticsStatusName(entry.getValue().get(0).getLogisticsstatus_name());
                    order.setLinkman(entry.getValue().get(0).getContact());
                    order.setContactPhone(entry.getValue().get(0).getContactphone());
                    order.setCustomerPhone(entry.getValue().get(0).getCustomerphone());
                    order.setExaminerName(entry.getValue().get(0).getExaminer_name());
                    order.setExaminerDate(entry.getValue().get(0).getExaminerdate());
                    order.setConsignorName(entry.getValue().get(0).getConsignor_name());
                    order.setConsignorDate(entry.getValue().get(0).getConsignordate());
                    order.setSourceVoucherTypeName(entry.getValue().get(0).getSourcevouchertype_name());
                    order.setSourceVoucherTypeCode(entry.getValue().get(0).getSourcevouchercode());
                    order.setDatasourceName(entry.getValue().get(0).getDatasource_name());
                    order.setSaleorderCode(entry.getValue().get(0).getSaleordercode());
                    order.setCollaborateVoucherCode(entry.getValue().get(0).getCollaboratevouchercode());
                    order.setIsstockRequestautoTransfe(entry.getValue().get(0).getIsstockrequestautotransfer());
                    order.setMemo(entry.getValue().get(0).getMemo());
                    order.setMaker(entry.getValue().get(0).getMaker());
                    order.setAgency(entry.getValue().get(0).getAgency());
                    order.setAuditor(entry.getValue().get(0).getAuditor());
                    order.setAuditedDate(entry.getValue().get(0).getAuditeddate());
                    order.setAuditedTime(entry.getValue().get(0).getAuditedtime());
                    order.setPrintCount(entry.getValue().get(0).getPrintcount());
                    order.setReviser(entry.getValue().get(0).getReviser());
                    order.setAmount(entry.getValue().get(0).getAmount());
                    order.setTotalTaxAmount(entry.getValue().get(0).getTotaltaxamount());
                    order.setVoucherstateName(entry.getValue().get(0).getVoucherstate_name());
                    order.setDocId(entry.getValue().get(0).getDocid());
                    order.setExternalCode(entry.getValue().get(0).getExternalcode());
                    order.setDeliverystateName(entry.getValue().get(0).getDeliverystate_name());
                    order.setPriuserdefnvc1(entry.getValue().get(0).getPriuserdefnvc1());
                    order.setPriuserdefnvc2(entry.getValue().get(0).getPriuserdefnvc2());
                    order.setPriuserdefnvc3(entry.getValue().get(0).getPriuserdefnvc3());
                    order.setPriuserdefnvc4(entry.getValue().get(0).getPriuserdefnvc4());
                    order.setPriuserdefnvc5(entry.getValue().get(0).getPriuserdefnvc5());
                    order.setPriuserdefnvc6(entry.getValue().get(0).getPriuserdefnvc6());
                    order.setPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm1())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm1()));
                    order.setPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm2())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm2()));
                    order.setPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm3())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm3()));
                    order.setPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm4())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm4()));
                    order.setPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm5())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm5()));
                    order.setPriuserdefdecm6(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm6())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm6()));
                    order.setTs(entry.getValue().get(0).getTs());
                    saleDispatchPageList.add(order);

                    // 按明细ID分组
                    Map<String, List<SaleDispatchVo>> detailsMap = entry.getValue().stream().collect(Collectors.groupingBy(SaleDispatchVo::getId));
                    for (Map.Entry<String, List<SaleDispatchVo>> detailsEntry : detailsMap.entrySet()) {
                        // 明细
                        SaleDispatchVo orderDetail = detailsEntry.getValue().get(0);
                        SaleDispatchDetails details = new SaleDispatchDetails();
                        details.setId(Long.valueOf(orderDetail.getId()));
                        details.setInventoryBarcode(orderDetail.getInvbarcode());
                        details.setBoxNumber(orderDetail.getBoxnumber());
                        details.setWarehouseName(orderDetail.getWarehouse_name());
                        details.setProjectName(orderDetail.getProject_name());
                        details.setBusinessAttributeName(orderDetail.getBusinessattribute_name());
                        details.setControlModeName(orderDetail.getControlmode_name());
                        details.setCusInventoryName(orderDetail.getCustomerinventoryprice());
                        details.setPartnerInventoryCode(orderDetail.getPartnerinventoryname());
                        details.setInventoryName(orderDetail.getInventory_name());
                        details.setInventoryCode(orderDetail.getInventory_code());
                        details.setInventorySpecification(orderDetail.getInventory_specification());
                        details.setInventoryPriuserdefnvc1(orderDetail.getInventory_priuserdefnvc1());
                        details.setInventoryPriuserdefnvc2(orderDetail.getInventory_priuserdefnvc2());
                        details.setInventoryPriuserdefnvc3(orderDetail.getInventory_priuserdefnvc3());
                        details.setInventoryPriuserdefnvc4(orderDetail.getInventory_priuserdefnvc4());
                        details.setInventoryPriuserdefnvc5(orderDetail.getInventory_priuserdefnvc5());
                        details.setInventoryPriuserdefdecm1(orderDetail.getInventory_priuserdefdecm1());
                        details.setInventoryPriuserdefdecm2(orderDetail.getInventory_priuserdefdecm2());
                        details.setInventoryPriuserdefdecm3(orderDetail.getInventory_priuserdefdecm3());
                        details.setInventoryPriuserdefdecm4(orderDetail.getInventory_priuserdefdecm4());
                        details.setInventoryPriuserdefdecm5(orderDetail.getInventory_priuserdefdecm5());
                        details.setUnitName(orderDetail.getUnit_name());
                        details.setArrivalQuantity(orderDetail.getArrivalquantity());
                        details.setQuantity(orderDetail.getQuantity());
                        details.setCompositionQuantity(orderDetail.getCompositionquantity());
                        details.setUnitExchangeRate(orderDetail.getChangerate());
                        details.setUnit2Name(orderDetail.getUnit2_name());
                        details.setArrivalQuantity2(orderDetail.getArrivalquantity2());
                        details.setQuantity2(orderDetail.getQuantity2());
                        details.setPackingCount(orderDetail.getPackingcount());
                        details.setUnitbyPackingName(orderDetail.getUnitbypacking_name());
                        details.setPackingCapacity(orderDetail.getPackingcapacity());
                        details.setBatch(orderDetail.getBatch());
                        details.setProductionDate(orderDetail.getProductiondate());
                        details.setInventoryExpired(orderDetail.getInventory_expired());
                        details.setInventoryExpiredunitNam(orderDetail.getInventory_expiredunit_nam());
                        details.setExpiryDate(orderDetail.getExpirydate());
                        details.setSncount(orderDetail.getSncount());
                        details.setSerialNumbers(orderDetail.getSerialnumbers());
                        details.setInventoryLocation(orderDetail.getInventorylocation());
                        details.setPrice(orderDetail.getPrice());
                        details.setDetailMemo(orderDetail.getDetailmemo());
                        details.setRetailPrice(orderDetail.getRetailprice());
                        details.setPrice2(orderDetail.getPrice2());
                        details.setAmount(orderDetail.getAmount());
                        details.setOrigPrice(orderDetail.getOrigprice());
                        details.setDiscountRate(orderDetail.getDiscountrate());
                        details.setTaxRate(orderDetail.getTaxrate());
                        details.setOrigSaleAmount(orderDetail.getOrigsaleamount());
                        details.setOrigSalePrice(orderDetail.getOrigsaleprice());
                        details.setOrigTax(orderDetail.getOrigtax());
                        details.setOrigTaxSaleAmount(orderDetail.getOrigtaxsaleamount());
                        details.setOrigTaxSalePrice(orderDetail.getOrigtaxsaleprice());
                        details.setSalePrice(orderDetail.getSaleprice());
                        details.setTaxSalePrice(orderDetail.getTaxsaleprice());
                        details.setOrigDiscount(orderDetail.getOrigdiscount());
                        details.setSaleAmount(orderDetail.getSaleamount());
                        details.setTax(orderDetail.getTax());
                        details.setTaxSaleAmount(orderDetail.getTaxsaleamount());
                        details.setDiscount(orderDetail.getDiscount());
                        details.setOrigSalePrice2(orderDetail.getOrigsaleprice2());
                        details.setOrigTaxSalePrice2(orderDetail.getOrigtaxsaleprice2());
                        details.setSalePrice2(orderDetail.getSaleprice2());
                        details.setTaxSalePrice2(orderDetail.getTaxsaleprice2());
                        details.setDispatchAdjust(orderDetail.getDispatchadjust());
                        details.setReceiveVoucherCode(orderDetail.getReceivevouchercode());
                        details.setIsmanualCost(orderDetail.getIsmanualcost());
                        details.setIspresent(orderDetail.getIspresent());
                        details.setExaminerName(orderDetail.getExaminer_name());
                        details.setExaminerDate(orderDetail.getExaminerdate());
                        details.setDatasourceName(orderDetail.getDatasource_name());
                        details.setSourceVoucherTypeName(orderDetail.getSourcevouchertype_name());
                        details.setSourceVoucherTypeCode(orderDetail.getSourcevouchercode());
                        details.setSaleorderCode(orderDetail.getSaleordercode());
                        details.setCumSaleDisQuantity(orderDetail.getCumulativesaledispatchquantity());
                        details.setCumSaleDisQuantity2(orderDetail.getCumulativesaledispatchquantity2());
                        details.setCumReturnQuantity(orderDetail.getCumreturnquantity());
                        details.setCumReturnQuantity2(orderDetail.getCumreturnquantity2());
                        details.setPriceStraTypenam(orderDetail.getPricestrategytypenam());
                        details.setPriceDimName(orderDetail.getPricedimension_name());
                        details.setPriceStraSchNames(orderDetail.getPricestrategyschemenames());
                        details.setProVoucherCodes(orderDetail.getPromotionvouchercodes());
                        details.setPriuserdefnvc1(orderDetail.getPriuserdefnvc1());
                        details.setPriuserdefnvc2(orderDetail.getPriuserdefnvc2());
                        details.setPriuserdefnvc3(orderDetail.getPriuserdefnvc3());
                        details.setPriuserdefnvc4(orderDetail.getPriuserdefnvc4());
                        details.setPriuserdefdecm1(Objects.isNull(orderDetail.getPriuserdefdecm1())? null : new BigDecimal(orderDetail.getPriuserdefdecm1()));
                        details.setPriuserdefdecm2(Objects.isNull(orderDetail.getPriuserdefdecm2())? null : new BigDecimal(orderDetail.getPriuserdefdecm2()));
                        details.setPriuserdefdecm3(Objects.isNull(orderDetail.getPriuserdefdecm3())? null : new BigDecimal(orderDetail.getPriuserdefdecm3()));
                        details.setPriuserdefdecm4(Objects.isNull(orderDetail.getPriuserdefdecm4())? null : new BigDecimal(orderDetail.getPriuserdefdecm4()));
                        details.setPubuserdefnvc1(orderDetail.getPubuserdefnvc1());
                        details.setPubuserdefnvc2(orderDetail.getPubuserdefnvc2());
                        details.setPubuserdefnvc3(orderDetail.getPubuserdefnvc3());
                        details.setPubuserdefnvc4(orderDetail.getPubuserdefnvc4());
                        details.setPubuserdefdecm1(Objects.isNull(orderDetail.getPubuserdefdecm1())? null : new BigDecimal(orderDetail.getPubuserdefdecm1()));
                        details.setPubuserdefdecm2(Objects.isNull(orderDetail.getPubuserdefdecm2())? null : new BigDecimal(orderDetail.getPubuserdefdecm2()));
                        details.setPubuserdefdecm3(Objects.isNull(orderDetail.getPubuserdefdecm3())? null : new BigDecimal(orderDetail.getPubuserdefdecm3()));
                        details.setPubuserdefdecm4(Objects.isNull(orderDetail.getPubuserdefdecm4())? null : new BigDecimal(orderDetail.getPubuserdefdecm4()));
                        saleDispatchDetails.add(details);
                    }
                }
                saveOrUpdateBatch(saleDispatchPageList);
                saleDispatchDetailsService.saveOrUpdateBatch(saleDispatchDetails);
            }
        } catch (ChanjetApiException e) {
            throw new RuntimeException(e);
        }

    }
}
